import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index'
import WxAuth from '@/pages/WxAuth'
import productList from '@/pages/productList'
import goodInfo from '@/pages/goodInfo'
import cart from '@/pages/cart'
import classGoods from '@/pages/classGoods'
import usercenter from '@/pages/userCenter'
import addressList from '@/pages/addressList'
import addressEdit from '@/pages/addressEdit'
import store from '@/store'
import {getAppid} from '../ajax'
import notFound from '../pages/notFound.vue'
Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index,
      meta:{
        title:'生鲜商城',
        footer:true
      }
    },
    {
      path:'/notFound',
      name:'notFound',
      component: notFound,
      meta:{
        title:'404'
      }
    },
    {
      path: '/WxAuth',
      name: 'WxAuth',
      component:WxAuth,
      meta:{
        title:'加载中...'
      }
    },
    {
      path: '/productList',
      name: 'productList',
      component:productList,
      meta:{
        back:true,
        title:'商品列表'
      }
    },
    {
      path:'/goodInfo',
      name:'goodInfo',
      component:goodInfo,
      meta:{
        back:true,
        title:'商品详情'
      }
    },
    {
      path:'/cart',
      name:'cart',
      component:cart,
      meta:{
        back:true,
        title:'购物车'
      }
    },
    {
      path:'/classGoods',
      name:'classGoods',
      component:classGoods,
      meta:{
        back:true,
        title:"商品分类"
      }
    },
    {
      path:'/usercenter',
      name:'usercenter',
      component:usercenter,
      meta:{
        back:true,
        title:"个人中心",
        footer:true
      }
    },
    {
      path:'/addressList',
      name:'addressList',
      component:addressList,
      meta:{
        title:'地址管理',
        back:true
      }
    },
    {
      path:'/addressEdit',
      name:'addressEdit',
      component:addressEdit,
      meta:{
        title:'地址新增/修改',
        back:true
      }
    }
  ] 
})
router.beforeEach((to, from, next) => {
  if (!/micromessenger/i.test(navigator.userAgent)) {
    next()
    return
  }
  //不要对 WxAuth 路由进行拦截，不进入 WxAuth 路由就拿不到微信返回的授权 code
  if (to.name == 'notFound') {
    next()
    return
  }
  if(!to.meta.footer){
    to.meta.footer = false;
  }
  if(!to.meta.back){
    to.meta.back = false;
  }
  let wxUserInfo = store.getters.wxUserInfo
  sessionStorage.setItem('wxRedirectUrl', window.location.origin)
  console.log(store.getters.wxUserInfo);
  if (!wxUserInfo) {
    //保存当前路由地址，授权后还会跳到此地址
    const data = {
      admin_id:to.query.admin_id?to.query.admin_id:store.getters.admin_id
    }
    getAppid(data).then(res=>{
      console.log(!res);
      if(!res){
        next('/notFound');
      }else{
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8527e3e11ca00660&redirect_uri=${window.location.origin}/WxAuth&response_type=code&scope=snsapi_userinfo&state=${to.query.admin_id}-${to.query.state}#wechat_redirect`
      }
    })
  } else {
    next()
  }
})
export default router;
